package com.ziyia.a5_2_3;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.loader.content.CursorLoader;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void phone(View view) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 200);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 200) {
            if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent, 100);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

            CursorLoader cursorLoader = new CursorLoader(this, data.getData(), null, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null && cursor.moveToFirst()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                String phone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

                String str = String.format("id = %s %n name = %s %n phone = %s", id, name, phone);
                new AlertDialog.Builder(this)
                        .setTitle("联系从信息")
                        .setMessage(str)
                        .create()
                        .show();
            }
    }

    public void OverrideType(View view) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("nsyw://aaa.bbb.ccc"));
        intent.setType("ns/yw");
        Toast.makeText(this, intent.toString(), Toast.LENGTH_SHORT).show();
    }

    public void OverrideData(View view) {
        Intent intent = new Intent();
        intent.setType("ns/yw");
        intent.setData(Uri.parse("nsyw://aaa.bbb.ccc"));
        Toast.makeText(this, intent.toString(), Toast.LENGTH_SHORT).show();
    }

    public void dataAndDataTest(View view) {
        Intent intent = new Intent();
        intent.setDataAndType(Uri.parse("nsyw://aaa.bbb.ccc"), "ns/yw");
        Toast.makeText(this, intent.toString(), Toast.LENGTH_SHORT).show();
    }

    public void dataTest(View view) {

//        Intent intent = new Intent();
//        intent.setData(Uri.parse("nsyw://"));

//        Intent intent = new Intent();
//        intent.setData(Uri.parse("nsyw://ziyia.cn"));


        Intent intent = new Intent();
        intent.setData(Uri.parse("nsyw://ziyia.cn:1000"));

//        Intent intent = new Intent();
//        intent.setData(Uri.parse("nsyw://ziyia.cn/get"));

//        Intent intent = new Intent();
//        intent.setData(Uri.parse("nsyw://ziyia.cn:2000/get"));
        startActivity(intent);
    }

    public void actionDataTest(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://ziyia.cn"));
        startActivity(intent);
    }

    public void home(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);

    }

    public void actionView(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://ziyia.cn"));
        startActivity(intent);
    }

    public void actionEdit(View view) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("content://com.android.contacts/contacts/2"));
        startActivity(intent);
    }

    public void actionDlal(View view) {
//        Intent intent = new Intent();

//        intent.setAction(Intent.ACTION_DIAL);
//        intent.setData(Uri.parse("tel:10086"));

//        intent.setAction(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse("content://contacts/people"));

//        startActivity(intent);


        Resources re = getResources();

//        XmlResourceParser xml = re.getXml(R.xml.users);
        InputStream xml = re.openRawResource(R.raw.test);
        new AlertDialog.Builder(this)
                .setTitle(getLocalClassName())
                .setMessage(xml.toString())
                .create()
                .show();


    }

    public void getxml(View view) throws XmlPullParserException, IOException {
        Resources resources = getResources();
        XmlResourceParser xml = resources.getXml(R.xml.users);

        StringBuilder builder = new StringBuilder();
        while (xml.getEventType() != XmlResourceParser.END_DOCUMENT) {

            if (xml.getEventType() == XmlResourceParser.START_TAG) {
                String name = xml.getName();
                if (name.equals("user")) {
                    String age = xml.getAttributeValue(0);
                    String sex = xml.getAttributeValue(1);
                    String username = xml.nextText();

                    builder.append("username = ").append(username).append('，');
                    builder.append("age = ").append(age).append("，");
                    builder.append("sex = ").append(sex).append("");
                }
                builder.append('\n');
            }
            xml.next();
        }
        new AlertDialog.Builder(this)
                .setTitle(getLocalClassName())
                .setMessage(builder.toString())
                .create()
                .show();
    }


    public void setTheme(View view) {

    }
}